package com.yc.mvc.util;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/8/10
 * Email haijunzhou@hnit.edu.cn
 */
import com.yc.mvc.Person;
import org.springframework.core.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Utils {
    private static DefaultParameterNameDisconverer dpnd = new DefaultParameterNameDisconverer();
    // 方式一
    // 返回方法的参数名列表
    // 参考: https://www.jb51.net/article/232059.htm
    public static String[] getParameterNames(Method method){
        return dpnd.getParameterNames(method);
    }

    // 设置对象属性
    public static Object setValue(Object object, String name, String svalue) {
        try{
            if(name.contains(".") == false){
                Field field = object.getClass().getDeclaredField(name);
                field.setAccessible(true);
                Object ovalue = null;
                if(String.class.isAssignableFrom(field.getType())){
                    ovalue = svalue;
                } else if(int.class.isAssignableFrom(field.getType())
                        || Integer.class.isAssignableFrom(field.getType())){
                    ovalue = Integer.valueOf(svalue);
                } else if(List.class.isAssignableFrom(field.getType())){
                    ovalue = field.get(object);
                    if(ovalue == null){
                        ovalue = new ArrayList();
                    }
                    ((List) ovalue).add(svalue);
                } else if(Person.class.isAssignableFrom(field.getType())){
                    ovalue = field.get(object);
                    if(ovalue == null){
                        ovalue = Person.class.newInstance();
                    }
                }
                field.set(object, ovalue);
                return ovalue;
            } else {
                // father.name =>  father  name
                int i = name.indexOf(".");
                String prefix = name.substring(0,i);
                String suffix = name.substring(i+1);
                Object fieldObject = setValue(object,prefix,null);
                setValue(fieldObject, suffix, svalue);
            }
        } catch (Exception e){
            throw new RuntimeException(e);
        }
        return null;
    }
//    // 方式二
//    public static void main(String[] args) throws NoSuchMethodException {
//        // 反射方式
//        StandardReflectionParameterNameDiscoverer nd = new StandardReflectionParameterNameDiscoverer();
//        // 本地变量表
//        LocalVariableTableParameterNameDiscoverer lv = new LocalVariableTableParameterNameDiscoverer();
//        // 获取方法对象
//        Method method = ParameterNameDiscovererTest.class.getDeclaredMethod("Name", String.class);
//        // 参数列表方式 1
//        System.out.println(Arrays.toString(nd.getParameterNames(method)));
//        // 参数列表方式 2
//        System.out.println(Arrays.toString(lv.getParameterNames(method)));
//    }
}
class DefaultParameterNameDisconverer extends PrioritizedParameterNameDiscoverer{
    public DefaultParameterNameDisconverer() {
        if(KotlinDetector.isKotlinReflectPresent()){
            addDiscoverer(new KotlinReflectionParameterNameDiscoverer());
        }
        addDiscoverer(new StandardReflectionParameterNameDiscoverer());
        addDiscoverer(new LocalVariableTableParameterNameDiscoverer());
    }
}

